package com.ywl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywl.dto.HouseDetailDto;
import com.ywl.entity.HouseDetail;
import com.ywl.entity.HouseInfo;
import com.ywl.mapper.HouseDetailMapper;
import com.ywl.service.HouseDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywl.service.HouseInfoService;
import com.ywl.utils.MyStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywl
 * @since 2022-08-26
 */
@Service
@Slf4j
public class HouseDetailServiceImpl extends ServiceImpl<HouseDetailMapper, HouseDetail> implements HouseDetailService {
    @Autowired
    private HouseInfoService houseInfoService;

    /**
     * 分页查询，带条件
     * @param houseDetailPage
     * @param city
     * @param content
     * @return
     */
    @Override
    public Page<HouseDetail> getPageByCondition(Page<HouseDetail> houseDetailPage, String city, String content) {
        //sql:select * from house_detail
        //     where city = '成都' and house_type like '%1室%' limit 0,5
        LambdaQueryWrapper<HouseDetail> qw = new LambdaQueryWrapper<>();
        qw.eq(HouseDetail::getCity,city);
        qw.like(StringUtils.isNotEmpty(content),HouseDetail::getTitle,content)
                .or().like(StringUtils.isNotEmpty(content),HouseDetail::getHouseType,content);
        super.page(houseDetailPage,qw);
        return houseDetailPage;
    }

    /**
     * 获得房屋详细信息
     * @param id
     * @return
     */
    @Override
    public HouseDetailDto getHouseDetailWithInfo(Long id) {
        //1.根据房屋id获取方房屋信息
        HouseDetail houseDetail = super.getById(id);
        //2.获取房屋户型id，获取房屋户型信息
        Long infoId = houseDetail.getInfoId();
        HouseInfo info = houseInfoService.getById(infoId);
        //3.创建houseDetailDto
        HouseDetailDto houseDetailDto = new HouseDetailDto();

        //4.把保存图片地址的string字符串进行转换，转换成List，并存入到imgs这个集合中
        BeanUtils.copyProperties(houseDetail,houseDetailDto,"imgs","img");
        houseDetailDto.setImgs(MyStringUtils.stringToList(houseDetail.getImgsString(),","));

        houseDetailDto.setInfo(info);
        log.info("imgs={}",houseDetailDto.getImgs().toString());
        return houseDetailDto;
    }

}
